package com.zxy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: Zxy
 * @description:
 * @date: 2021-07-20 19:58
 */
@RestController
@RefreshScope
public class DemoController {

    private final Logger log= LoggerFactory.getLogger(DemoController.class);

    @GetMapping("/demo")
    @SentinelResource(value = "demo",blockHandler = "customerBlockHandler",fallback = "customerFallBack")
    public String demo(){
        log.info("demo ok");
        return "demo ok";
    }

    /* 对应上面的blockHandler处理 必须加BlockException */
    public String customerBlockHandler(BlockException e){
        e.printStackTrace();
        return "被限流了...";
    }

    /* 这个函数对应上面的fallback */
    public String customerFallBack(){
        return "fallback函数回显自定义限流";
    }

}
